import json
import logging
from datetime import datetime, timedelta
from alert_utils.aliyun_info_get import get_slow_log_count
from alert_utils.aliyun_info_get import aliyun_client_init
from alert_utils.aliyun_info_get import get_slow_log_detail
from alert_utils.aliyun_info_get import slow_log_msg_trim
from alert_utils.aliyun_info_get import format_msg
from info_config import access_key_id
from info_config import access_key_secret
from info_config import endpoint
from info_config import instance_id_list
from info_config import feishu_url
from info_config import slow_log_alert_size
from info_config import msg_query_alert_thresold

from info_config import mins_interval
from info_config import msg_send_for_debug
from alert_utils.alert_msg import message_send

# Set up logging
logging.basicConfig(
    filename="exec.log", level=logging.INFO, format="%(asctime)s %(message)s"
)


def get_instance_id_list():
    return


def slow_log_alert():
    aliyun_client = aliyun_client_init(
        access_key_id=access_key_id,
        access_key_secret=access_key_secret,
        endpoint=endpoint,
    )
    for instance_id in instance_id_list:
        end_time = datetime.utcnow()
        start_time = end_time - timedelta(minutes=mins_interval)
        slow_log_count, slow_log_record_list = get_slow_log_detail(
            aliyun_clinet=aliyun_client,
            instance_id=instance_id,
            start_time=start_time.strftime("%Y-%m-%dT%H:%MZ"),
            end_time=end_time.strftime("%Y-%m-%dT%H:%MZ"),
            msg_log_size=slow_log_alert_size,
        )
        logging.info(f"Slow log count for instance {instance_id}: {slow_log_count}")
        
        if slow_log_count > 0:
            sql_list = slow_log_msg_trim(slow_log_record_list,msg_query_alert_thresold)
            sql_msg = format_msg(sql_list)
            message_send(
                msg=f"Instance {instance_id} has {slow_log_count} slow logs in the last {mins_interval} minutes.\n sql: {sql_msg}",
                title="RDS Slow Log Alert",
                feishu_url=feishu_url,
            )
        else:
            logging.info(
                f"No slow logs for instance {instance_id} in the last 15 minutes."
            )
        if msg_send_for_debug == 1:
            sql_list = slow_log_msg_trim(slow_log_record_list,msg_query_alert_thresold)
            sql_msg = format_msg(sql_list)
            message_send(
                msg=f"Instance {instance_id} has {slow_log_count} slow logs in the last {mins_interval} minutes.\n sql: {sql_msg}",
                title="RDS Slow Log Alert",
                feishu_url=feishu_url,
            )


def get_slow_log_backup():
    aliyun_client = aliyun_client_init(
        access_key_id=access_key_id,
        access_key_secret=access_key_secret,
        endpoint=endpoint,
    )
    for instance_id in instance_id_list:
        end_time = datetime.utcnow()
        start_time = end_time - timedelta(minutes=mins_interval)
        slow_log_count = get_slow_log_count(
            aliyun_clinet=aliyun_client,
            instance_id=instance_id,
            start_time=start_time.strftime("%Y-%m-%dT%H:%MZ"),
            end_time=end_time.strftime("%Y-%m-%dT%H:%MZ"),
        )
        logging.info(f"Slow log count for instance {instance_id}: {slow_log_count}")
        if slow_log_count > 0:
            message_send(
                msg=f"Instance {instance_id} has {slow_log_count} slow logs in the last {mins_interval} minutes.",
                title="RDS Slow Log Alert",
                feishu_url=feishu_url,
            )
        else:
            logging.info(
                f"No slow logs for instance {instance_id} in the last 15 minutes."
            )
        if msg_send_for_debug == 1:
            message_send(
                msg=f"Instance {instance_id} has {slow_log_count} slow logs in the last {mins_interval} minutes.",
                title="RDS Slow Log Alert",
                feishu_url=feishu_url,
            )


def main():
    slow_log_alert()


if __name__ == "__main__":
    main()
